import { openDB } from 'idb'
import { toRaw } from 'vue'
import type { friend } from '@/api/messages/type'

// //封装本地存储数据与读取好友数据的方法
// export const SET_FRIENDS = (friends: friend[]) => {
//     localStorage.setItem('FRIENDS', JSON.stringify(friends))
// }

// export const GET_FRIENDS = () => {
//     const localFriends = localStorage.getItem('FRIENDS')
//     //检查存储的值是否存在，然后将其解析为对象
//     return localFriends ? JSON.parse(localFriends) : []
// }

// export const REMOVE_FRIENDS = () => {
//     localStorage.removeItem('FRIENDS')
// }

// //封装本地存储正在聊天的好友数据的方法
// export const SET_CHATTINGFRIEND = (chattingFriend: friend) => {
//     localStorage.setItem('CHATTINGFRIEND', JSON.stringify(chattingFriend))
// }

// export const GET_CHATTINGFRIEND = () => {
//     const chattingFriend = localStorage.getItem('CHATTINGFRIEND')
//     //检查存储的值是否存在，然后将其解析为对象
//     return chattingFriend ? JSON.parse(chattingFriend) : []
// }

// export const REMOVE_CHATTINGFRIEND = () => {
//     localStorage.removeItem('CHATTINGFRIEND')
// }

//封装本地存储数据与读取聊天的所有好友数据的方法
export const SET_CHATLISTFRIENDS = async (chatListFriends: friend[]) => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保'directories'对象存储已经被创建
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    await db.put('directories', chatListFriends, 'CHATLISTFRIENDS')
}

export const GET_CHATLISTFRIENDS = async () => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保'directories'对象存储已经被创建
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    const chatListFriends = await db.get('directories', 'CHATLISTFRIENDS')
    return chatListFriends || []
}

export const REMOVE_CHATLISTFRIENDS = async () => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保对象仓库已经存在
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    await db.delete('directories', 'CHATLISTFRIENDS')
}

//封装本地存储数据与读取好友数据的方法
export const SET_FRIENDS = async (friends: friend[]) => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保'directories'对象存储已经被创建
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    await db.put('directories', friends, 'FRIENDS')
}

export const GET_FRIENDS = async () => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保'directories'对象存储已经被创建
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    const friends = await db.get('directories', 'FRIENDS')
    return friends || []
}

export const REMOVE_FRIENDS = async () => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保对象仓库已经存在
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    await db.delete('directories', 'FRIENDS')
}

//封装本地存储正在聊天的好友数据的方法
export const SET_CHATTINGFRIEND = async (chattingFriend: friend) => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保'directories'对象存储已经被创建
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    await db.put('directories', toRaw(chattingFriend), 'CHATTINGFRIEND')
}

export const GET_CHATTINGFRIEND = async () => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保'directories'对象存储已经被创建
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    const chattingFriend = await db.get('directories', 'CHATTINGFRIEND')
    return chattingFriend || {}
}

export const REMOVE_CHATTINGFRIEND = async () => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保对象仓库已经存在
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    await db.delete('directories', 'CHATTINGFRIEND')
}

//封装本地存储通讯录当前展示的好友数据的方法
export const SET_CONTACTSFRIEND = async (contactsFriend: friend) => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保'directories'对象存储已经被创建
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    await db.put('directories', toRaw(contactsFriend), 'CONTACTSFRIEND')
}

export const GET_CONTACTSFRIEND = async () => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保'directories'对象存储已经被创建
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    const contactsFriend = await db.get('directories', 'CONTACTSFRIEND')
    return contactsFriend || {}
}

export const REMOVE_CONTACTSFRIEND = async () => {
    const db = await openDB('directoryDB', 2, {
        upgrade(db) {
            //确保对象仓库已经存在
            if (!db.objectStoreNames.contains('directories')) {
                db.createObjectStore('directories')
            }
        },
    })
    await db.delete('directories', 'CONTACTSFRIEND')
}
